package com.qf.sclg.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**后台进行跨域处理，cros
 * Created with IntelliJ IDEA.
 * User: zdk
 * Date: 2021/10/13 9:44
 * Description:
 * Version: V1.0
 */
@Configuration
public class CorsConfig implements WebMvcConfigurer {
    @Autowired
    private AuthorizedInterceptor authorizedInterceptor;
//    重写方法
    private  static  final String[] METHODS = {"GET","POST","PUT","PATCH","DELETE"};
    @Override
    public void addCorsMappings(CorsRegistry registry) {
//        拦截所有请求   .……链式操作
        registry.addMapping("/**")
//                所有源
                .allowedOriginPatterns("*")
//                允许请求携带请求头信息
                .allowedHeaders("*")
                .allowedMethods(METHODS)
//                是否允许携带cookie
                .allowCredentials(true)
//                时间范围内不用发送预检请求
                .maxAge(60);
    }
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(authorizedInterceptor);
    }
}
